package demo.android.myapplication.demos.views

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import cn.android.basic.*
import demo.android.myapplication.R

/**
 * <br>createBy Gw
 * <br>createTime: 2021/7/9 09:42
 */
class FoldView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val bitmap:Bitmap = decodeBitmap(R.mipmap.ic_launcher_copy, 100.dp.int)
    private val rect = Rect()
    private val camera = Camera()

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        camera.save()
        //setLocation(x, y ,z) 中 x, y两个参数没有用, 都填0, 如需移动摄像机, 可以移动画布代替
//        camera.setLocation(0, 0, -8f)
        camera.rotateX(45f)

        canvas.save()
        canvas.rotate(30f, centerX, centerY)
        rect.set(0, 0, width, centerY.int)
        canvas.clipRect(rect)
        canvas.rotate(-30f, centerX, centerY)
        canvas.drawBitmap(bitmap, centerX - bitmap.centerX, centerY - bitmap.centerY, paint)
        canvas.restore()

        canvas.save()
        canvas.rotate(30f, centerX, centerY)
        rect.set(0, centerY.int, width, height)
        canvas.translate(centerX, centerY)
        camera.applyToCanvas(canvas)
        canvas.translate(-centerX, -centerY)
        canvas.clipRect(rect)
        canvas.rotate(-30f, centerX, centerY)
        canvas.drawBitmap(bitmap, centerX - bitmap.centerX, centerY - bitmap.centerY, paint)
        canvas.restore()

        camera.restore()
    }
}